package com.ssm.anno.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张富玮
 * @create 2022-01-29 10:02
 */
@Configuration // 指定配置类
@ComponentScan("com.ssm.anno.mapper")
@MapperScan("com.ssm.anno.mapper") // Mapper 接口扫描
public class SpringMapperConfig {
    /**
     * 创建 Druid 数据源
     *
     * @return
     */
    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/book");
        dataSource.setUsername("root");
        dataSource.setPassword("020327");
        return dataSource;
    }

    /**
     * 整合 MyBatisPlus
     *
     * @param dataSource
     * @param globalConfig
     * @return
     */
    @Bean
    public MybatisSqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 实体类驼峰命名转换
        sqlSessionFactoryBean.setTypeAliasesPackage("com.ssm.anno.entity");
        // 引入全局策略配置
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);
        // 插件拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        List<InnerInterceptor> interceptors = new ArrayList<InnerInterceptor>();
        interceptors.add(paginationInnerInterceptor);
        interceptor.setInterceptors(interceptors);
        sqlSessionFactoryBean.setPlugins(interceptor);

        return sqlSessionFactoryBean;
    }

    /**
     * 全局策略配置
     *
     * @param dbConfig
     * @return
     */
    @Bean
    public GlobalConfig getGlobalConfig(GlobalConfig.DbConfig dbConfig) {
        GlobalConfig globalConfig = new GlobalConfig();
        // 引入数据库配置
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }

    /**
     * 数据库配置
     *
     * @return
     */
    @Bean
    public GlobalConfig.DbConfig getDbConfig() {
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        // 自增主键
        dbConfig.setIdType(IdType.AUTO);
        // 数据库表名前缀
        dbConfig.setTablePrefix("t_");
        // 逻辑删除
        dbConfig.setLogicDeleteField("logic_delete_flag");
        dbConfig.setLogicDeleteValue("-1");
        dbConfig.setLogicNotDeleteValue("1");
        return dbConfig;
    }

}
